package client.comm.baoding.adapter

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.Paint
import android.text.Layout
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.view.setPadding
import androidx.recyclerview.widget.DiffUtil
import client.comm.baoding.R
import client.comm.baoding.api.bean.GoodsIndex
import client.comm.baoding.databinding.LayoutTjhwBinding
import client.comm.baoding.ui.GoodsDetailActivity
import client.comm.baoding.ui.GoodsDetailTradeActivity
import client.comm.baoding.utils.TextTool
import client.comm.commlib.base.BasePagingAdapter
import client.comm.commlib.base.PagingViewHoder
import client.comm.commlib.dipToPx
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions

/**
 * 精选商品
 */
class TjGood2Adapter(val context: Context, val manager: RequestManager) :

    BasePagingAdapter<GoodsIndex.TjGoods>(object : DiffUtil.ItemCallback<GoodsIndex.TjGoods>() {
        override fun areItemsTheSame(
            oldItem: GoodsIndex.TjGoods,
            newItem: GoodsIndex.TjGoods
        ): Boolean {
            return oldItem.goods_id == newItem.goods_id
        }

        override fun areContentsTheSame(
            oldItem: GoodsIndex.TjGoods,
            newItem: GoodsIndex.TjGoods
        ): Boolean {
            return oldItem == newItem
        }
    }) {

    override fun getItemLayout(): Int {
        return R.layout.layout_tjhw
    }

    override fun bindItem(holder: PagingViewHoder, t: Any) {
        val binding = holder.binding as LayoutTjhwBinding
        val bean = t as GoodsIndex.TjGoods
        binding.bean = bean

        manager.load(bean.goods_img)
//            .apply(RequestOptions().error(R.mipmap.ic_launcher))
//            .thumbnail(0.1f)
//            .skipMemoryCache(true) //跳过内存缓存
//            .diskCacheStrategy(DiskCacheStrategy.NONE) //不缓冲disk硬盘中
            .into(binding.img)

        TextTool.getBuilder("¥").setAlign(Layout.Alignment.ALIGN_CENTER).setBold().setTextSize(14)
            .append(bean.price.toString()).setForegroundColor(Color.parseColor("#E81837")).setBold().setTextSize(16)
            .into(binding.tvPrice)

//        binding.tvPriceOld.apply {
//            setPaintFlags(binding.tvPriceOld.getPaintFlags() or Paint.STRIKE_THRU_TEXT_FLAG)
//        }

        binding.llLabel.removeAllViews()
        for (e in bean.goods_label) {
            binding.llLabel.addView(getLabel(e))
        }

        binding.apply {
            event = this@TjGood2Adapter
        }
    }

    //设置商品label标签
    private fun getLabel(str: String): View? {
        val child = TextView(context)
        child.textSize = 12f
        var drawId = when (str) {
            "热卖" -> R.drawable.label_bg_red1_0
            "新品" -> R.drawable.label_bg_green1_0
            "爱心商品" -> R.drawable.label_bg_blue1_0
            else -> null
        }
        if (drawId != null) {
            child.background = context.getDrawable(drawId)
        }

        child.setTextColor(context.resources.getColor(R.color.white))
        child.setText(str)
        child.setPadding(dipToPx(context.resources, 2f).toInt())
        // 定义LayoutParam
        val params: RelativeLayout.LayoutParams =
            RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        params.rightMargin = dipToPx(context.resources, 10f).toInt()
        child.layoutParams = params
        // 调用带有LayoutParams参数的addView方法
        return child//mContainer.addView(child, params)
    }

//    private fun getView(): View? {
//        // TODO 动态添加布局(xml方式)
//        val lp: FrameLayout.LayoutParams = LayoutParams(
//            dip2px(this, 100),
//            dip2px(this, 500)
//        )
//        val inflater3: LayoutInflater = LayoutInflater.from(this)
//        val view: View = inflater3.inflate(R.layout.layout, null)
//        view.layoutParams = lp
//        return view
//    }

    fun itemClick(bean: GoodsIndex.TjGoods) {
//        if (bean.is_type == 1) {
//            context.startActivity(Intent(context, GoodsDetailTradeActivity::class.java).apply {
//                putExtra("goodsId", "${bean.goods_id}")
//            })
//        } else {
            context.startActivity(Intent(context, GoodsDetailActivity::class.java).apply {
                putExtra("goodsId", "${bean.goods_id}")
            })
//        }

    }

}